home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_11 / phillip2 / filter.c < prev    next >
C/C++ Source or Header  |  1993-06-23  |  15KB  |  598 lines

  1.  
  2.     /***********************************************
  3.     *
  4.     *    file d:\cips\filter.c
  5.     *
  6.     *    Functions: This file contains
  7.     *       filter_image
  8.     *       median_filter
  9.     *       high_pixel
  10.     *       low_pixel
  11.     *       setup_filters
  12.     *       get_filter_options
  13.     *       median_of
  14.     *       sort_elements
  15.     *       swap
  16.     *
  17.     *    Purpose:
  18.     *       These functions implement several
  19.     *       types of basic spatial frequency
  20.     *       filters.
  21.     *
  22.     *    External Calls:
  23.     *       wtiff.c - round_off_image_size
  24.     *                 create_file_if_needed
  25.     *                 write_array_into_tiff_image
  26.     *       tiff.c - read_tiff_header
  27.     *       rtiff.c - read_tiff_image
  28.     *       numcvrt.c - get_integer
  29.     *
  30.     *
  31.     *    Modifications:
  32.     *       15 February 1992 - created
  33.     *
  34.     *************************************************/
  35.  
  36. #include "cips.h"
  37.  
  38.  
  39.      /*******************************************
  40.      *
  41.      *   Define the filter masks.
  42.      *
  43.      *******************************************/
  44.  
  45. short lpf_filter_6[3][3] =
  46.    { {0, 1, 0},
  47.      {1, 2, 1},
  48.      {0, 1, 0}};
  49.  
  50. short lpf_filter_9[3][3] =
  51.    { {1, 1, 1},
  52.      {1, 1, 1},
  53.      {1, 1, 1}};
  54.  
  55. short lpf_filter_10[3][3] =
  56.    { {1, 1, 1},
  57.      {1, 2, 1},
  58.      {1, 1, 1}};
  59.  
  60. short lpf_filter_16[3][3] =
  61.    { {1, 2, 1},
  62.      {2, 4, 2},
  63.      {1, 2, 1}};
  64.  
  65. short lpf_filter_32[3][3] =
  66.    { {1,  4, 1},
  67.      {4, 12, 4},
  68.      {1,  4, 1}};
  69.  
  70. short hpf_filter_1[3][3] =
  71.    { { 0, -1,  0},
  72.      {-1,  5, -1},
  73.      { 0, -1,  0}};
  74.  
  75. short hpf_filter_2[3][3] =
  76.    { {-1, -1, -1},
  77.      {-1,  9, -1},
  78.      {-1, -1, -1}};
  79.  
  80. short hpf_filter_3[3][3] =
  81.    { { 1, -2,  1},
  82.      {-2,  5, -2},
  83.      { 1, -2,  1}};
  84.  
  85.  
  86.  
  87.  
  88.  
  89.      /*******************************************
  90.      *
  91.      *   filter_image(...
  92.      *
  93.      *   This function filters an image by using
  94.      *   a single 3x3 mask.
  95.      *
  96.      *******************************************/
  97.  
  98.  
  99. filter_image(in_name, out_name, the_image, out_image,
  100.              il, ie, ll, le, filter, type)
  101.    char   in_name[], out_name[];
  102.    int    il, ie, ll, le, type;
  103.    short  filter[3][3],
  104.           the_image[ROWS][COLS],
  105.           out_image[ROWS][COLS];
  106.  
  107. {
  108.    int    a, b, d, i, j, k,
  109.           length, max, sum, width;
  110.    struct tiff_header_struct image_header;
  111.  
  112.  
  113.    create_file_if_needed(in_name, out_name, out_image);
  114.  
  115.    read_tiff_header(in_name, &image_header);
  116.  
  117.    d = type;
  118.    if(type == 2 || type == 3) d = 1;
  119.  
  120.    max = 255;
  121.    if(image_header.bits_per_pixel == 4)
  122.       max = 16;
  123.  
  124.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  125.  
  126.          /* Do convolution over image array */
  127.    printf("\n");
  128.    for(i=1; i<ROWS-1; i++){
  129.       if( (i%10) == 0) printf("%d ", i);
  130.       for(j=1; j<COLS-1; j++){
  131.          sum = 0;
  132.          for(a=-1; a<2; a++){
  133.             for(b=-1; b<2; b++){
  134.                sum = sum +
  135.                      the_image[i+a][j+b] *
  136.                      filter[a+1][b+1];
  137.             }
  138.          }
  139.          sum               = sum/d;
  140.          if(sum < 0)   sum = 0;
  141.          if(sum > max) sum = max;
  142.          out_image[i][j]   = sum;
  143.  
  144.       }  /* ends loop over j */
  145.    }  /* ends loop over i */
  146.  
  147.    fix_edges(out_image, 1);
  148.  
  149.    write_array_into_tiff_image(out_name, out_image,
  150.                                il, ie, ll, le);
  151.  
  152. }  /* ends filter_image */
  153.  
  154.  
  155.  
  156.  
  157.      /*******************************************
  158.      *
  159.      *   high_pixel(..
  160.      *
  161.      *   This function replaces the pixel at
  162.      *   the center of a 3x3, 5x5, etc. area
  163.      *   with the max for that area.
  164.      *
  165.      *******************************************/
  166.  
  167. high_pixel(in_name, out_name, the_image, out_image,
  168.            il, ie, ll, le, size)
  169.    char   in_name[], out_name[];
  170.    int    il, ie, ll, le, size;
  171.    short  the_image[ROWS][COLS],
  172.           out_image[ROWS][COLS];
  173. {
  174.    int    a, b, count, i, j, k,
  175.           length, sd2, sd2p1, ss, width;
  176.    short  *elements;
  177.    struct tiff_header_struct image_header;
  178.  
  179.    sd2   = size/2;
  180.    sd2p1 = sd2 + 1;
  181.  
  182.       /**********************************************
  183.       *
  184.       *   Allocate the elements array large enough
  185.       *   to hold size*size shorts.
  186.       *
  187.       **********************************************/
  188.  
  189.    ss       = size*size;
  190.    elements = (short *) malloc(ss * sizeof(short));
  191.  
  192.    create_file_if_needed(in_name, out_name, out_image);
  193.  
  194.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  195.  
  196.       /***************************
  197.       *
  198.       *   Loop over image array
  199.       *
  200.       ****************************/
  201.  
  202.    printf("\n");
  203.    for(i=sd2; i<ROWS-sd2; i++){
  204.       if( (i%10) == 0) printf("%d ", i);
  205.       for(j=sd2; j<COLS-sd2; j++){
  206.          count = 0;
  207.          for(a=-sd2; a<sd2p1; a++){
  208.             for(b=-sd2; b<sd2p1; b++){
  209.                elements[count] = the_image[i+a][j+b];
  210.                count++;
  211.             }
  212.          }
  213.          sort_elements(elements, &ss);
  214.          out_image[i][j] = elements[ss-1];
  215.       }  /* ends loop over j */
  216.    }  /* ends loop over i */
  217.  
  218.    fix_edges(out_image, sd2);
  219.  
  220.    write_array_into_tiff_image(out_name, out_image,
  221.                                il, ie, ll, le);
  222.  
  223.    free(elements);
  224.  
  225. }  /* ends high_pixel */
  226.  
  227.  
  228.  
  229.  
  230.      /*******************************************
  231.      *
  232.      *   low_pixel(..
  233.      *
  234.      *   This function replaces the pixel at
  235.      *   the center of a 3x3, 5x5, etc. area
  236.      *   with the min for that area.
  237.      *
  238.      *******************************************/
  239.  
  240.  
  241. low_pixel(in_name, out_name, the_image, out_image,
  242.           il, ie, ll, le, size)
  243.    char   in_name[], out_name[];
  244.    int    il, ie, ll, le, size;
  245.    short  the_image[ROWS][COLS],
  246.           out_image[ROWS][COLS];
  247.  
  248. {
  249.    int    a, b, count, i, j, k,
  250.           length, sd2, sd2p1, ss, width;
  251.    short  *elements;
  252.    struct tiff_header_struct image_header;
  253.  
  254.    sd2   = size/2;
  255.    sd2p1 = sd2 + 1;
  256.  
  257.       /**********************************************
  258.       *
  259.       *   Allocate the elements array large enough
  260.       *   to hold size*size shorts.
  261.       *
  262.       **********************************************/
  263.  
  264.    ss       = size*size;
  265.    elements = (short *) malloc(ss * sizeof(short));
  266.  
  267.    create_file_if_needed(in_name, out_name, out_image);
  268.  
  269.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  270.  
  271.       /***************************
  272.       *
  273.       *   Loop over image array
  274.       *
  275.       ****************************/
  276.  
  277.    printf("\n");
  278.    for(i=sd2; i<ROWS-sd2; i++){
  279.       if( (i%10) == 0) printf("%d ", i);
  280.       for(j=sd2; j<COLS-sd2; j++){
  281.          count = 0;
  282.          for(a=-sd2; a<sd2p1; a++){
  283.             for(b=-sd2; b<sd2p1; b++){
  284.                elements[count] = the_image[i+a][j+b];
  285.                count++;
  286.             }
  287.          }
  288.          sort_elements(elements, &ss);
  289.          out_image[i][j] = elements[0];
  290.       }  /* ends loop over j */
  291.    }  /* ends loop over i */
  292.  
  293.    fix_edges(out_image, sd2);
  294.  
  295.    write_array_into_tiff_image(out_name, out_image,
  296.                                il, ie, ll, le);
  297.  
  298.    free(elements);
  299.  
  300. }  /* ends low_pixel */
  301.  
  302.  
  303.  
  304.  
  305.      /*******************************************
  306.      *
  307.      *   median_filter(..
  308.      *
  309.      *   This function performs a median filter
  310.      *   on an image using a size (3x3, 5x5, etc.)
  311.      *   specified in the call.
  312.      *
  313.      *******************************************/
  314.  
  315. median_filter(in_name, out_name, the_image, out_image,
  316.               il, ie, ll, le, size)
  317.    char   in_name[], out_name[];
  318.    int    il, ie, ll, le, size;
  319.    short  the_image[ROWS][COLS],
  320.           out_image[ROWS][COLS];
  321.  
  322. {
  323.    int    a, b, count, i, j, k,
  324.           length, sd2, sd2p1, ss, width;
  325.    short  *elements;
  326.    struct tiff_header_struct image_header;
  327.  
  328.    sd2   = size/2;
  329.    sd2p1 = sd2 + 1;
  330.  
  331.       /**********************************************
  332.       *
  333.       *   Allocate the elements array large enough
  334.       *   to hold size*size shorts.
  335.       *
  336.